package com.homedo.microservice.open.service.gateway.manager.web.remote.api.impl;

import com.google.common.collect.Lists;
import com.homedo.common.bean.data.RequestData;
import com.homedo.common.bean.data.ResponseData;
import com.homedo.common.bean.vo.Order;
import com.homedo.common.bean.vo.Pagination;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.manager.web.api.IRouteDomainServiceRemote;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.RouteDomainAddAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.RouteDomainPageAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.RouteDomainUpdateAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.RouteDomainAResp;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.RouteDomainPageAResp;
import com.homedo.microservice.open.service.gateway.manager.web.convent.CommonConvent;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.RouteDomain;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.enhanced.RouteDomainEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.query.RouteDomainQuery;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.service.RouteDomainService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RouteDomainServiceRemoteImpl implements IRouteDomainServiceRemote {
    @Autowired
    private RouteDomainService routeDomainService;

    @Override
    public ResponseData<RouteDomainPageAResp> list(RouteDomainPageAReq routeDomainPageAReq) {
        ResponseData<RouteDomainPageAResp> responseData = new ResponseData<>();

        if (routeDomainPageAReq.getPage() == 0) {
            routeDomainPageAReq.setPage(1);
        }
        if (routeDomainPageAReq.getSize() == 0) {
            routeDomainPageAReq.setSize(Integer.MAX_VALUE);//默认查询全部
        }

        RouteDomainQuery routeDomainQuery = new RouteDomainQuery();
        routeDomainQuery.setApplicationNames(Lists.newArrayList(routeDomainPageAReq.getApplicationName()));
        if (Detect.notEmpty(routeDomainPageAReq.getLikeDomain())) {
            routeDomainQuery.setLikeDomain(routeDomainPageAReq.getLikeDomain());
        }
        if (routeDomainPageAReq.getStatus() != null) {
            routeDomainQuery.setStatuses(Lists.newArrayList(routeDomainPageAReq.getStatus()));
        }

        Pagination<RouteDomainEnhanced> pagination = new Pagination(routeDomainPageAReq.getPage(), routeDomainPageAReq.getSize(), true);
        pagination.addOrder(new Order(RouteDomain.COLUMN_STATUS, Order.ORDER_DESC), RouteDomain.class)
                .addOrder(new Order(RouteDomain.COLUMN_CREATE_TIME, Order.ORDER_DESC), RouteDomain.class);

        pagination = routeDomainService.getPagination(routeDomainQuery, pagination, null);

        RouteDomainPageAResp routeDomainPageAResp = new RouteDomainPageAResp();
        routeDomainPageAResp.setCount(pagination.getCount());
        routeDomainPageAResp.setPage(pagination.getIndex());
        routeDomainPageAResp.setSize(pagination.getSize());

        if (Detect.notEmpty(pagination.getItems())) {
            routeDomainService.appendRouteEnhanceds(pagination.getItems(), null);
            routeDomainPageAResp.setItems(CommonConvent.convent2RouteDomainAResps(pagination.getItems()));
        }

        responseData.setData(routeDomainPageAResp);

        return responseData;
    }

    @Override
    public ResponseData<RouteDomainAResp> detail(@PathVariable("id") Integer id) {
        ResponseData<RouteDomainAResp> responseData = new ResponseData<>();

        RouteDomainEnhanced routeDomainEnhanced = routeDomainService.getEnhanced(id, null);
        responseData.setData(CommonConvent.convent2RouteDomainAResp(routeDomainEnhanced));
        return responseData;
    }

    @Override
    public ResponseData update(@RequestBody RequestData<RouteDomainUpdateAReq> requestData) {
        ResponseData responseData = new ResponseData();

        RouteDomainUpdateAReq routeDomainUpdateAReq = requestData.getRequest();

        routeDomainService.updateRouteDomain(routeDomainUpdateAReq.getId(), routeDomainUpdateAReq.getDomain(), routeDomainUpdateAReq.getComment(),
                routeDomainUpdateAReq.getStatus(), null);

        return responseData;
    }

    @Override
    public ResponseData<RouteDomainAResp> add(@RequestBody RequestData<RouteDomainAddAReq> requestData) {
        ResponseData<RouteDomainAResp> responseData = new ResponseData<>();

        RouteDomainAddAReq routeDomainAddAReq = requestData.getRequest();
        RouteDomain routeDomain = new RouteDomain();
        BeanUtils.copyProperties(routeDomainAddAReq, routeDomain);

        routeDomain = routeDomainService.addRouteDomain(routeDomain, null);

        responseData.setData(CommonConvent.convent2RouteDomainAResp((RouteDomainEnhanced) routeDomainService.convent2Enhanced(routeDomain)));

        return responseData;
    }

    @Override
    public ResponseData delete(@PathVariable("id") Integer id) {
        ResponseData responseData = new ResponseData();

        routeDomainService.deleteRouteDomain(id, null);

        return responseData;
    }
}
